package com.bw.ivideo.testmvi.home.view

import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.bw.ivideo.databinding.FragmentHomeBinding
import com.bw.ivideo.testmvi.home.intent.HomeSimpleIntent
import com.bw.ivideo.testmvi.home.state.HomeSimpleTypeState
import com.bw.ivideo.testmvi.home.viewmodel.HomeViewModel
import com.bw.lib_common.base.BaseMVIFragment
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

/**
 * @author ytx
 * @date 2025-10-17 09:32
 * @description
 */
@AndroidEntryPoint
class HomeFragment:BaseMVIFragment() {
    private val binding: FragmentHomeBinding by lazy{
        FragmentHomeBinding.inflate(layoutInflater)
    }
    private lateinit var mViewModel: HomeViewModel
    override fun generateViewModel() {
        mViewModel = ViewModelProvider(this)[HomeViewModel::class.java]
    }

    override fun getFragmentRootView(): View {
       return binding.root
    }

    /**
     * 发送意图
     */
    override fun initData() {
        super.initData()
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.RESUMED){
                mViewModel.intents.send(HomeSimpleIntent.getSimpleType)
            }
        }
    }

    /**
     * 处理状态
     */
    override fun handleState() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.RESUMED){
                mViewModel.simpleTypeStateFlow_.collect{
                    when(it){
                        is HomeSimpleTypeState.Init -> {

                        }
                        is HomeSimpleTypeState.Loading -> {
                            //加载中
                        }
                        is HomeSimpleTypeState.Empty -> {
                            //空数据
                            showMsg("空数据")
                        }
                        is HomeSimpleTypeState.Success -> {
                            //成功
                            showMsg("成功"+it.data.toString())
                        }
                        is HomeSimpleTypeState.Error -> {
                            //失败
                            showMsg("失败"+it.message)
                        }
                    }
                }
            }
        }

    }
}